package com.iamteer.cupband;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class BondingActivity extends Activity {

    private ImageView ivBack;
    private TextView tvEnableGoalSetting;
    public static Activity bondingActivity;
    public static WiperSwitch swBondingEnable;
//    static Fragment bondingFragment;
//    static Fragment bondingBeforeFragment;
//    static Fragment bondingAfterFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bonding);

        bondingActivity = this;

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
                ActionBar.LayoutParams.MATCH_PARENT,
                ActionBar.LayoutParams.MATCH_PARENT,
                Gravity.CENTER);
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View titleView = inflater.inflate(R.layout.activity_bonding_title, null);
        actionBar.setCustomView(titleView, lp);

        actionBar.setDisplayShowHomeEnabled(false);//去掉导航
        actionBar.setDisplayShowTitleEnabled(false);//去掉标题
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setDisplayShowCustomEnabled(true);

        tvEnableGoalSetting = (TextView) findViewById(R.id.tvEnableGoalSetting);

        ivBack = (ImageView) actionBar.getCustomView().findViewById(R.id.ivBack);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        swBondingEnable = (WiperSwitch) findViewById(R.id.swBondingEnable);
        swBondingEnable.setChecked(MainActivity.cupBondingEnable);

///*        if(savedInstanceState == null){
//            bondingFragment = new BondingFragment();
//            bondingBeforeFragment = new BondingBeforeFragment();
//            bondingAfterFragment = new BondingAfterFragment();
//
//            getFragmentManager().beginTransaction()
//                    .add(R.id.fragment, bondingFragment)
//                    .commit();
//            getFragmentManager().beginTransaction()
//                    .add(R.id.fragment, bondingBeforeFragment)
//                    .commit();
//        }*/

        ChangeFragment(swBondingEnable.nowStatus);

        swBondingEnable.setOnChangedListener(new WiperSwitch.OnChangedListener() {
            @Override
            public void OnChanged(WiperSwitch wiperSwitch, boolean checkState) {
//                if(checkState == false){
//
//                    if (MainActivity.mState == MainActivity.UART_PROFILE_CONNECTED) {
//                        new AlertDialog.Builder(BondingActivity.this).setTitle("系统提示")          //设置对话框标题
//                                .setMessage("此操作将会断开与瓶环的连接，您的喝水记录将会失去，确定吗？")        //设置显示的内容
//                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {            //添加确定按钮
//                                    @Override
//                                    public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
//                                        //断开设备连接
////                                        unbindService(BondingFragment.mServiceConnection);
////                                        mService.stopSelf();
////                                        mService= null;
//                                        finish();
//                                    }
//                                })
//                                .setNegativeButton("返回", new DialogInterface.OnClickListener() {//添加返回按钮
//                                    @Override
//                                    public void onClick(DialogInterface dialog, int which) {//响应事件
//                                        swBondingEnable.setChecked(true);
//                                        MainActivity.cupBondingEnable = swBondingEnable.nowStatus;
//                                        System.out.println("Cancel......");
//                                    }
//                                })
//                                .show();//在按键响应事件中显示此对话框
//                    }
//                }
                if (checkState) {
                    tvEnableGoalSetting.setText("开启");
                } else {
                    tvEnableGoalSetting.setText("关闭");
                }
                ChangeFragment(checkState);
            }
        });

    }

    private void ChangeFragment(boolean checkState) {
        if(MainActivity.mState == MainActivity.UART_PROFILE_CONNECTED){
            getFragmentManager().beginTransaction()
                    .replace(R.id.fragment, new BondingAfterFragment())
                    .commit();
            swBondingEnable.setEnabled(false);
            swBondingEnable.setAlpha(0.4f);
            return;
        }

        MainActivity.cupBondingEnable = checkState;
        if (checkState) {
            getFragmentManager().beginTransaction()
                    .replace(R.id.fragment, new BondingFragment())
                    .commit();
//                    getFragmentManager().beginTransaction().hide(bondingBeforeFragment).commit();
//                    getFragmentManager().beginTransaction().show(bondingFragment).commit();
        } else {
            getFragmentManager().beginTransaction()
                    .replace(R.id.fragment, new BondingBeforeFragment())
                    .commit();
//                    getFragmentManager().beginTransaction().hide(bondingFragment).commit();
//                    getFragmentManager().beginTransaction().show(bondingBeforeFragment).commit();
        }
        SharedPreferences sp = getSharedPreferences(MainActivity.SharedPreferencesName, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(MainActivity.KEY_cupBondingEnable, checkState);
        editor.apply();
    }
}


